home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / Excel / Sheet.jar / sheet / Cell.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-09-03  |  7.2 KB  |  321 lines

  1. package sheet;
  2.  
  3. import javax.microedition.lcdui.Command;
  4. import javax.microedition.lcdui.Display;
  5. import javax.microedition.lcdui.Displayable;
  6. import javax.microedition.lcdui.Font;
  7. import javax.microedition.lcdui.Graphics;
  8. import javax.microedition.lcdui.Image;
  9. import javax.microedition.lcdui.TextBox;
  10.  
  11. final class Cell {
  12.    static final String[] colors = new String[]{"White", "Light Gray", "Black"};
  13.    static final int BG_WHITE = 0;
  14.    static final int BG_LGRAY = 1;
  15.    static final int BG_BLACK = 2;
  16.    static final String[] aligns = new String[]{"Default", "Right", "Center", "Left"};
  17.    static final int AL_DEFAULT = 0;
  18.    static final int AL_RIGHT = 1;
  19.    static final int AL_CENTER = 2;
  20.    static final int AL_LEFT = 3;
  21.    private static CellContainer parent;
  22.    private static Calculate calc = new Calculate((1)null);
  23.    private byte column;
  24.    private byte row;
  25.    private short colorIdx = 0;
  26.    private short alignIdx = 0;
  27.    private String formula;
  28.    private Operand result;
  29.    private boolean calculated;
  30.    private String errMsg;
  31.    private Image image;
  32.  
  33.    public Cell(CellContainer var1, byte var2, byte var3) {
  34.       parent = var1;
  35.       this.column = var2;
  36.       this.row = var3;
  37.       this.calculated = true;
  38.       this.render();
  39.    }
  40.  
  41.    public boolean isData() {
  42.       return this.colorIdx != 0 || this.formula != null;
  43.    }
  44.  
  45.    public byte getRow() {
  46.       return this.row;
  47.    }
  48.  
  49.    public byte getColumn() {
  50.       return this.column;
  51.    }
  52.  
  53.    public void setColor(short var1) {
  54.       if (this.colorIdx != var1) {
  55.          this.colorIdx = var1;
  56.          parent.notifyChanged();
  57.          this.render();
  58.       }
  59.  
  60.    }
  61.  
  62.    public void setAlign(short var1) {
  63.       if (this.alignIdx != var1) {
  64.          this.alignIdx = var1;
  65.          parent.notifyChanged();
  66.          this.render();
  67.       }
  68.  
  69.    }
  70.  
  71.    public void render() {
  72.       if (this.isData()) {
  73.          short var1 = parent.getColumnWidth(this.column);
  74.          short var2 = parent.getRowHeight(this.row);
  75.          Font var3 = Font.getFont(64, 0, 8);
  76.          int var4 = var2 - var3.getHeight();
  77.          int var5 = 1;
  78.          String var6 = this.errMsg == null ? (this.result == null ? "" : Operand.getText(this.result)) : this.errMsg;
  79.          int var7 = var3.stringWidth(var6);
  80.          if (var7 > 0) {
  81.             int var8 = var1 - 3;
  82.             int var9 = this.alignIdx;
  83.             if (var7 > var8) {
  84.                var7 = var8;
  85.             }
  86.  
  87.             if (var9 == 0) {
  88.                var9 = this.result != null && Operand.isNumber(this.result) ? 1 : 3;
  89.             }
  90.  
  91.             switch (var9) {
  92.                case 1:
  93.                   var5 = var8 - var7;
  94.                   break;
  95.                case 2:
  96.                   var5 = (var8 - var7) / 2;
  97.                   break;
  98.                case 3:
  99.                   var5 = 1;
  100.             }
  101.          }
  102.  
  103.          this.image = Image.createImage(var1, var2);
  104.          Graphics var12 = this.image.getGraphics();
  105.          var12.setColor(0);
  106.          var12.setFont(var3);
  107.          switch (this.colorIdx) {
  108.             case 1:
  109.                int var13 = 1 - var1 % 2;
  110.  
  111.                for(int var10 = 1; var10 < var2; var10 += 2) {
  112.                   var12.drawLine(0, var10, var2 - 1 - var10, var2 - 1);
  113.                   var12.drawLine(var1 - 1, var10 - var13, var1 - 1 - var10 + var13, 0);
  114.                }
  115.  
  116.                for(int var11 = 1; var11 <= var1 - var2; var11 += 2) {
  117.                   var12.drawLine(var11, 0, var11 + var2 - 1, var2 - 1);
  118.                }
  119.  
  120.                var12.setColor(16777215);
  121.                var12.drawString(var6, var5 + 1, var4 + 1, 20);
  122.                var12.setColor(0);
  123.                break;
  124.             case 2:
  125.                var12.fillRect(0, 0, var1, var2);
  126.                var12.setColor(16777215);
  127.          }
  128.  
  129.          var12.drawString(var6, var5, var4, 20);
  130.       } else {
  131.          this.image = null;
  132.       }
  133.  
  134.    }
  135.  
  136.    public int paint(Graphics var1, int var2, int var3) {
  137.       if (this.image != null) {
  138.          var1.drawImage(this.image, var2, var3, 20);
  139.       }
  140.  
  141.       return this.colorIdx == 2 ? 16777215 : 0;
  142.    }
  143.  
  144.    public String getName() {
  145.       char var1 = (char)(65 + this.column);
  146.       return var1 + String.valueOf(this.row + 1);
  147.    }
  148.  
  149.    public String getDetails() {
  150.       StringBuffer var1 = new StringBuffer();
  151.       if (this.formula != null) {
  152.          var1.append("Formula: ");
  153.          var1.append(this.formula);
  154.          var1.append("\n");
  155.          if (this.errMsg != null) {
  156.             var1.append("Error: ");
  157.             var1.append(this.errMsg);
  158.          } else if (this.result != null) {
  159.             var1.append("Result: ");
  160.             var1.append(Operand.getText(this.result));
  161.          } else {
  162.             var1.append("No result");
  163.          }
  164.       }
  165.  
  166.       return var1.toString();
  167.    }
  168.  
  169.    public void notCalculated() {
  170.       this.result = null;
  171.       this.calculated = false;
  172.    }
  173.  
  174.    public boolean isCalculated() {
  175.       return this.calculated;
  176.    }
  177.  
  178.    public void paste(Cell var1) {
  179.       if (var1 != null) {
  180.          this.formula = var1.formula;
  181.          this.colorIdx = var1.colorIdx;
  182.          this.alignIdx = var1.alignIdx;
  183.          parent.notifyChanged();
  184.          parent.calculate();
  185.       }
  186.  
  187.    }
  188.  
  189.    public boolean calculate() {
  190.       this.calculated = true;
  191.       this.errMsg = null;
  192.       if (this.formula != null) {
  193.          CellCalc var1 = new CellCalc(calc);
  194.          char var2 = this.formula.charAt(0);
  195.          if (var2 == '=' && this.formula.length() > 1) {
  196.             try {
  197.                this.result = var1.calculate(this.formula, (short)1, (short)this.formula.length());
  198.             } catch (IllegalArgumentException var4) {
  199.                this.calculated = false;
  200.                this.result = null;
  201.                this.errMsg = ((Throwable)var4).getMessage();
  202.             }
  203.          } else if (var2 == '\'') {
  204.             this.result = new Operand(this.formula.substring(1), false);
  205.          } else {
  206.             this.result = new Operand(this.formula, true);
  207.          }
  208.       }
  209.  
  210.       this.render();
  211.       return this.calculated;
  212.    }
  213.  
  214.    public void edit(String var1, Backable var2, Display var3) {
  215.       byte var4 = 0;
  216.       if (var1 == null) {
  217.          var1 = this.formula;
  218.       } else if (var1.length() > 0 && Character.isDigit(var1.charAt(0))) {
  219.          var4 = 2;
  220.       }
  221.  
  222.       String var5 = this.getName();
  223.       Command var6 = new Command("Ok", 4, 0);
  224.       Command var7 = new Command("Cancel", 3, 0);
  225.       Command var8 = new Command("Help", 5, 0);
  226.       TextBox var9 = new TextBox(var5, var1, 50, var4);
  227.       ((Displayable)var9).setCommandListener(new 1(this, var8, var3, var6, var2));
  228.       ((Displayable)var9).addCommand(var6);
  229.       ((Displayable)var9).addCommand(var7);
  230.       ((Displayable)var9).addCommand(var8);
  231.       var3.setCurrent(var9);
  232.    }
  233.  
  234.    public boolean appendChar(char var1, boolean var2) {
  235.       if (var1 == '\b' && this.formula != null) {
  236.          int var3 = this.formula.length();
  237.          if (var3 > 1) {
  238.             this.formula = this.formula.substring(0, var3 - 1);
  239.          } else {
  240.             this.formula = null;
  241.          }
  242.       } else if (!var2 && this.formula != null) {
  243.          this.formula = this.formula + var1;
  244.       } else {
  245.          this.formula = String.valueOf(var1);
  246.       }
  247.  
  248.       parent.notifyChanged();
  249.       parent.calculate();
  250.       return this.formula != null && this.formula.length() != 0;
  251.    }
  252.  
  253.    public byte[] serialize(int var1) {
  254.       byte[] var2;
  255.       if (this.isData() && var1 == 1) {
  256.          byte[] var3 = this.formula == null ? new byte[0] : this.formula.getBytes();
  257.          var2 = new byte[var3.length + 4];
  258.          var2[0] = this.column;
  259.          var2[1] = this.row;
  260.          var2[2] = (byte)this.colorIdx;
  261.          var2[3] = (byte)this.alignIdx;
  262.          System.arraycopy(var3, 0, var2, 4, var3.length);
  263.       } else {
  264.          var2 = null;
  265.       }
  266.  
  267.       return var2;
  268.    }
  269.  
  270.    public static Cell deserialize(byte[] var0, CellContainer var1, int var2) {
  271.       Cell var3;
  272.       if (var2 == 1 && var0 != null) {
  273.          var3 = new Cell(var1, var0[0], var0[1]);
  274.          var3.colorIdx = (short)var0[2];
  275.          var3.alignIdx = (short)var0[3];
  276.          var3.formula = new String(var0, 4, var0.length - 4);
  277.          if (var3.formula.length() == 0) {
  278.             var3.formula = null;
  279.          }
  280.  
  281.          var3.calculated = false;
  282.       } else {
  283.          var3 = null;
  284.       }
  285.  
  286.       return var3;
  287.    }
  288.  
  289.    static CellContainer access$000() {
  290.       return parent;
  291.    }
  292.  
  293.    static String access$100(Cell var0) {
  294.       return var0.errMsg;
  295.    }
  296.  
  297.    static boolean access$200(Cell var0) {
  298.       return var0.calculated;
  299.    }
  300.  
  301.    static Operand access$300(Cell var0) {
  302.       return var0.result;
  303.    }
  304.  
  305.    static Calculate access$500() {
  306.       return calc;
  307.    }
  308.  
  309.    static String access$600(Cell var0) {
  310.       return var0.formula;
  311.    }
  312.  
  313.    static String access$602(Cell var0, String var1) {
  314.       return var0.formula = var1;
  315.    }
  316.  
  317.    static String access$102(Cell var0, String var1) {
  318.       return var0.errMsg = var1;
  319.    }
  320. }
  321.